package com.easyidea.its.domain.repository.system.impl;

import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.easyidea.its.domain.model.system.Role;
import com.easyidea.its.domain.repository.base.impl.BaseRepositoryImpl;
import com.easyidea.its.domain.repository.system.RoleRepository;
import com.easyidea.its.util.ParamsUtilities;

/**
 * 
 * @author hukn
 * 
 */
public class RoleRepositoryImpl extends BaseRepositoryImpl<Role> implements
		RoleRepository {

	private static final int ENABLED_STATUS = 1;

	/**
	 * @see com.infowarelab.elearning.domain.repository.Role.RoleRepository
	 *      #getRolesByStatus(String,Integer, int, int)
	 */
	public Collection<Role> getRolesByStatus(String roleName,
			Integer status, int start, int pageSize) {
		Criteria ca = getCriteriaByStatus(roleName, status);

		if (start > 0) {
			ca.setFirstResult(start);
		}
		if (pageSize > 0) {
			ca.setMaxResults(pageSize);
		}
		ca.addOrder(Order.asc("id"));

		return (Collection<Role>) ca.list();
	}

	/**
	 * @see com.infowarelab.elearning.domain.repository.Role.RoleRepository
	 *      #getRoleCountByStatus(String, Integer)
	 */
	public int getRoleCountByStatus(String roleName, Integer status) {
		Criteria ca = getCriteriaByStatus(roleName, status);
		ca.setProjection(Projections.countDistinct("id"));
		return (Integer) ca.uniqueResult();
	}

	/**
	 * 根据状态返回查询对象
	 * 
	 * @param status
	 * @return
	 */
	private Criteria getCriteriaByStatus(String roleName, Integer status) {
		Criteria ca = this.createCriteria();
		if (status != -1) {
			ca.add(Restrictions.eq("status", status));
		}
		if (roleName != null && !roleName.equals("")) {
			ca.add(Restrictions.ilike("roleName", ParamsUtilities.dealWithReservedSqlChar(roleName), MatchMode.ANYWHERE));
		}
		return ca;
	}

//	public int deleteRoleRelationPermissionsByRoleId(Integer roleId) {
//
//		return this.getSession().createSQLQuery(
//				"delete from el_sa_role_permission_ref where role_id = "
//						+ roleId).executeUpdate();
//
//	}

	public List<Role> getAllRoles() {
		Criteria ca = this.createCriteria();
		ca.add(Restrictions.eq("status", ENABLED_STATUS));
		ca.addOrder(Order.asc("id"));
		return (List<Role>) ca.list();
	}

//	/*
//	 * (non-Javadoc)
//	 * 
//	 * @seecom.infowarelab.elearning.domain.repository.Role.
//	 * RoleRepository#getUserRole(java.lang.Integer)
//	 */
//	public Role getUserRole(Integer userId) {
//		String hql = "select r from com.infowarelab.elearning.domain.model.User as u inner join u.roleId as r where u.id=?";
//
//		List<Role> roles = (List<Role>) this.listBeansByHql(
//				hql, -1, -1, userId);
//		if (roles.size() > 0) {
//			return roles.get(0);
//		}
//		return null;
//	}
	public Collection<Role> getPlatformRolesByStatus(String roleName,
			Integer status, int start, int pageSize) {
		Criteria ca = getCriteriaByStatus(roleName, status);

		if (start > 0) {
			ca.setFirstResult(start);
		}
		if (pageSize > 0) {
			ca.setMaxResults(pageSize);
		}
		ca.addOrder(Order.desc("id"));

		return (Collection<Role>) ca.list();
	}	
	public int getPlatformRoleCountByStatus(String roleName, Integer status) {
		Criteria ca = getCriteriaByStatus(roleName, status);
		ca.setProjection(Projections.countDistinct("id"));
		return (Integer) ca.uniqueResult();
	}
	public int deleteRoleRelationPermissionsByRoleId(Integer roleId) {
		Role role = this.getBeanById(roleId);
		role.setPermissionList(null);
		 this.getCurrentSession().update(role);
		return 1;
		//return this.getCurrentSession().createSQLQuery("delete from sys_role_permission_ref where role_id = "+ roleId).executeUpdate();

	}	
}
